<?php
  class panel_controller_panel extends Base
  {
      private $pModel; 
      
      public function __construct()
      {
          $this -> pModel = new panelQuery();
          parent::__construct();
          $this -> Authenticate();
          $this -> getUserData();
          $this -> setWhere('2', 'Mój panel', 'moj-sos');
      }
      
      public function init()
      {
          if(isset($_POST['search']) && $_POST['search'] !="")
          {
            $this -> view -> assign('searchValue', $_POST['search']);
            $activities = $this -> pModel -> getUserActivitiesSearch($_SESSION[PREFIX]['user']['id'], $_POST['search']);   
          }
          else
          {
            $activities = $this -> pModel -> getUserActivities($_SESSION[PREFIX]['user']['id']);              
          }
            $this -> view -> assign('actMenu', 'H');
            $this -> view -> assign('activities', $activities);
      }
      
      public function addNewJobForm()
      {
          $this -> getUserData();
          $this -> setWhere('3', 'Nowe zlecenie', 'panel/nowe-zlecenie');       
      }
      
      public function mySms()
      {
            $this -> view -> assign('actMenu', 'PS');
            $this -> setWhere('3', 'Moje smsy', 'panel/moje-smsy');    
      }
      
      public function more()
      {

      }
      
      public function myData()
      {
          $this -> view -> assign('actMenu', 'MD');
      }
      
      public function favourites()
      {
         $this -> view -> assign('actMenu', 'F');  
      }
      
      public function mySerwices()
      {
         $mySerwices = $this -> pModel -> getMyServices($_SESSION[PREFIX]['user']['id']);
         $this -> view -> assign('mySerwices', $mySerwices);
         $this -> view -> assign('actMenu', 'MS');  
      }
      
      public function editData()
      {
          $message = null;
          if(!is_numeric($_POST['phone']))
          {
              $message['phone'] = "Numer telefonu posiada niedozwolone znaki. Dozwolone znaki to cyfry od 0-9.";
          }
      
          if($message == null)
          {
              $el = array();
              $el['name'] = $_POST['name'];
              $el['surname'] = $_POST['surname'];
              $el['phone'] = $_POST['phone'];
              $el['latitude'] = $_POST['latitude'];
              $el['longitude'] = $_POST['longitude'];
              $where = array();
              $where['id'] = $_SESSION[PREFIX]['user']['id'];
              $this -> common -> update($el, 'users', $where);
              
              $message['content'] = "Twoje dane zostały zaktualizowane.";
              $message['type'] = "success";
          }
          else
          {
            $message['content'] = "Wystąpiły błady w formularzu. Popraw je aby iść dalej";
            $message['type'] = "error";
          }
          
          $this -> setMessage($message);
          $this -> FriendlyLink(SITE_ADDRESS.'panel/moje-dane');
      }
      
      public function myMessages()
      {
        $this -> setWhere('3', 'Moje wiadomosci', 'panel/moje-wiadomosci');   
      }
      
      public function addNewJob()
      {
 
      }
      
      private function getUserData()
      {
          $where = array();
          $where['id'] = $_SESSION[PREFIX]['user']['id'];
          $user = $this -> common -> select('users', '*', $where);
          $this -> view -> assign("userData", $user[0]);       
      }
  }
?>
